package thoven.achievement.net.ideapluginachievement.util

import com.intellij.openapi.vfs.VirtualFile

/**
 * 编程语言检测工具
 */
object LanguageDetector {
    
    // 代码文件扩展名集合
    private val CODE_EXTENSIONS = setOf(
        // Java系
        "java", "kt", "kts", "groovy", "scala",
        // Web前端
        "js", "jsx", "ts", "tsx", "vue", "html", "css", "scss", "sass", "less",
        // Python
        "py", "pyw", "pyx",
        // C/C++
        "c", "cpp", "cc", "cxx", "h", "hpp", "hxx",
        // C#
        "cs",
        // Go
        "go",
        // Rust
        "rs",
        // PHP
        "php",
        // Ruby
        "rb",
        // Swift
        "swift",
        // Objective-C
        "m", "mm",
        // Shell
        "sh", "bash", "zsh",
        // SQL
        "sql",
        // XML/JSON/YAML
        "xml", "json", "yaml", "yml",
        // Markdown
        "md",
        // 其他
        "dart", "r", "lua", "perl", "vim"
    )
    
    // 扩展名到语言的映射
    private val EXTENSION_TO_LANGUAGE = mapOf(
        // Java系
        "java" to "Java",
        "kt" to "Kotlin",
        "kts" to "Kotlin Script",
        "groovy" to "Groovy",
        "scala" to "Scala",
        
        // Web前端
        "js" to "JavaScript",
        "jsx" to "JavaScript (JSX)",
        "ts" to "TypeScript",
        "tsx" to "TypeScript (TSX)",
        "vue" to "Vue",
        "html" to "HTML",
        "css" to "CSS",
        "scss" to "SCSS",
        "sass" to "Sass",
        "less" to "Less",
        
        // Python
        "py" to "Python",
        "pyw" to "Python",
        "pyx" to "Cython",
        
        // C/C++
        "c" to "C",
        "cpp" to "C++",
        "cc" to "C++",
        "cxx" to "C++",
        "h" to "C/C++ Header",
        "hpp" to "C++ Header",
        "hxx" to "C++ Header",
        
        // C#
        "cs" to "C#",
        
        // Go
        "go" to "Go",
        
        // Rust
        "rs" to "Rust",
        
        // PHP
        "php" to "PHP",
        
        // Ruby
        "rb" to "Ruby",
        
        // Swift
        "swift" to "Swift",
        
        // Objective-C
        "m" to "Objective-C",
        "mm" to "Objective-C++",
        
        // Shell
        "sh" to "Shell",
        "bash" to "Bash",
        "zsh" to "Zsh",
        
        // SQL
        "sql" to "SQL",
        
        // XML/JSON/YAML
        "xml" to "XML",
        "json" to "JSON",
        "yaml" to "YAML",
        "yml" to "YAML",
        
        // Markdown
        "md" to "Markdown",
        
        // 其他
        "dart" to "Dart",
        "r" to "R",
        "lua" to "Lua",
        "perl" to "Perl",
        "vim" to "VimScript"
    )
    
    /**
     * 判断文件是否是代码文件
     */
    fun isCodeFile(file: VirtualFile): Boolean {
        val extension = file.extension?.lowercase() ?: return false
        return CODE_EXTENSIONS.contains(extension)
    }
    
    /**
     * 检测文件的编程语言
     */
    fun detectLanguage(file: VirtualFile): String {
        val extension = file.extension?.lowercase() ?: return "Unknown"
        return EXTENSION_TO_LANGUAGE[extension] ?: "Unknown"
    }
    
    /**
     * 获取所有支持的编程语言
     */
    fun getSupportedLanguages(): Set<String> {
        return EXTENSION_TO_LANGUAGE.values.toSet()
    }
} 
